package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.task;

import com.iwhalecloud.citybrain.flow.platform.manager.application.event.EventCallURL;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.ActivityCompletedPostProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.CommonTaskEventProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.exception.EventCallSuspendException;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.eventconfig.DeployedEventConfigDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.eventconfig.EventConfigTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.eventconfig.EventMethodEnum;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.impl.FlowableActivityEventImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Objects;

/**
 * 结束节点的后置
 *
 * @author songxf
 * @date 2021/08/04
 */
@Slf4j
@Component
public class SendBusinessSystemAfterEndNodeCompletedProcessor implements ActivityCompletedPostProcessor {
    @Autowired
    private CommonTaskEventProcessor commonTaskEventProcessor;

    @Override
    public void process(FlowableActivityEventImpl activityEvent) throws EventCallSuspendException {
        if (!"endEvent".equals(activityEvent.getActivityType())) {
            return;
        }
        log.info("节点结束后置事件, activityName=>{}, processDefinitionId=>{}, activityId=>{} ", activityEvent.getActivityName(), activityEvent.getProcessDefinitionId(), activityEvent.getActivityId());
        //1.根据任务信息，查询关联的配置信息
        List<DeployedEventConfigDomain> eventConfigs = commonTaskEventProcessor.resolveEventConfigList(
                activityEvent, EventConfigTypeEnum.ACTIVITY_COMPLETED.name());
        if (Objects.isNull(eventConfigs)) {
            log.info("当前任务没有前置url，不需要执行");
            return;
        }
        // 对每条记录都执行一次
        for (DeployedEventConfigDomain config: eventConfigs) {
            String types = config.getCallbackType();
            switch (EventMethodEnum.queryEnum(types)) {
                case HTTP_URL:
                    new EventCallURL(activityEvent
                            , config
                    ).doProcess("结束节点后置");
                    break;
                case GROOVY:
                    // TODO
                    break;
                case JAVA_CLASS:
                    // TODO
                    break;
                default:
                    log.error("不支持的触发类型");
                    break;
            }
        }
    }
}
